//
//  NIGestureLockView.m
//  AIWAYS
//
//  Created by Navinfo on 2018/6/22.
//  Copyright © 2018年 wlq. All rights reserved.
//

#import "NIGestureLockView.h"
#import "TQGestureLockDrawManager.h"
#import "TQGestureLockView.h"
#import "TagDefines.h"
#import "DeviceDefine.h"

@implementation NIGestureLockView{
    __weak IBOutlet UIView *contentView;
    __weak IBOutlet UIView *bgView;
    
    __weak IBOutlet UIView *gestureLockBgView;
    TQGestureLockView *lockView;
    __weak IBOutlet NSLayoutConstraint *bottomConstraint;
    __weak IBOutlet UIButton *changeButton;
    __weak IBOutlet UIButton *forgotButton;
    __weak IBOutlet UIButton *forgotButton2;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self awakeFromNib];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
    
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 2*50.0f;
    CGFloat height = width;
    
    CGFloat diameter = width/(1.5 * 3);
    CGFloat spacing = 10.0f;
    TQGestureLockDrawManager *drawManager = [TQGestureLockDrawManager defaultManager];
    drawManager.circleDiameter = diameter;
    drawManager.edgeSpacingInsets = UIEdgeInsetsMake(spacing, spacing, spacing, spacing);
    drawManager.solidCircleFactor = 0.25;
    drawManager.bridgingLineWidth = 0.5;
    drawManager.hollowCircleBorderWidth = 0.5;
    lockView = [[TQGestureLockView alloc] initWithFrame:CGRectMake(0, 0, width, height) drawManager:drawManager];
    lockView.delegate = self;
    [gestureLockBgView addSubview:lockView];
    
    bottomConstraint.constant = BOTTOM_CONSTRAINT;
}


- (void)setHidden:(BOOL)hidden{
    [super setHidden:hidden];
    if (!hidden) {
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        NSArray *viewTags = @[@TAG_NOTIFICATIONINWINDOW,@TAG_MENUINWINDOW,@TAG_SECURITYPASSWORDINWINDOW,@TAG_SECURITYPASSWORDBGINWINDOW,@TAG_LOADINGINWINDOW];
        NSNumber *number;
        for (int i = 0; i < viewTags.count; i++) {
            number = [viewTags objectAtIndex:i];
            UIView *view = [window viewWithTag:number.integerValue];
            
            
            if (view) {
                [view.superview bringSubviewToFront:view];
                [window setNeedsLayout];
                [window layoutIfNeeded];
            }
        }
    }
    
    if (self.tag != TAG_SECURITYPASSWORDINWINDOW) {
        [self setTag:TAG_SECURITYPASSWORDINWINDOW];
    }
    if (hidden) {
//        [self clearPassword];
    }
}

- (IBAction)onCloseButtonClick:(id)sender {
    if (self.delegate) {
        [self.delegate viewWillClose];
    }
    [self setHidden:YES];
}

- (IBAction)onForgotButtonClick:(id)sender {
    if (self.delegate) {
        [self setHidden:YES];
        [self.delegate forgotButtonDidClick];
    }
}

- (IBAction)onForgotButton2Click:(id)sender {
    if (self.delegate) {
        [self setHidden:YES];
        [self.delegate gestureForgotButtonDidClick];
    }
}

- (IBAction)onChangeButtonClick:(id)sender {
    if (self.delegate) {
        [self setHidden:YES];
        [self.delegate changeButtonDidClick];
    }
}

- (void)gestureLockView:(TQGestureLockView *)gestureLockView finalRightSecurityCodeSting:(NSString *)securityCodeSting
{
    [gestureLockView clearsContextBeforeDrawing];
    [self.delegate gestureDidInputFinish:securityCodeSting];
}

- (void)clearView{
    [lockView setNeedsDisplayGestureLockErrorState:NO];
}

- (void)hiddenChangeButton:(BOOL)hidden{
    changeButton.hidden = hidden;
    forgotButton.hidden = hidden;
    forgotButton2.hidden = !hidden;
}

@end
